<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Getting A Set of Documents With a Query</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="mongo.tutorial.criteria.html">« Setting Criteria for a Query</a></li>
      <li style="float: right;"><a href="mongo.tutorial.indexes.html">Creating An Index »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="mongo.tutorial.html">Tutorial</a></li>
    <li>Getting A Set of Documents With a Query</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="mongo.tutorial.multi.query" class="section">
   <h2 class="title">Getting A Set of Documents With a Query</h2>
   <p class="para">
    We can use the query to get a set of documents from our collection. For
    example, if we wanted to get all documents where <code class="literal">&quot;i&quot;</code>
    &gt; <code class="literal">50</code>, we could write:
   </p>
   <div class="example" id="mongo.tutorial.multi.query-example">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$connection&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$collection&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$connection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">database</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">collectionName</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">=&nbsp;array(&nbsp;</span><span style="color: #DD0000">"i"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(&nbsp;</span><span style="color: #DD0000">'$gt'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">50&nbsp;</span><span style="color: #007700">)&nbsp;);&nbsp;</span><span style="color: #FF8000">//note&nbsp;the&nbsp;single&nbsp;quotes&nbsp;around&nbsp;'$gt'<br /></span><span style="color: #0000BB">$cursor&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$collection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">find</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">);<br /><br />while&nbsp;(&nbsp;</span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">hasNext</span><span style="color: #007700">()&nbsp;)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getNext</span><span style="color: #007700">()&nbsp;);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     which should print the documents where <code class="literal">&quot;i&quot;</code> &gt;
     <code class="literal">50</code>. We could also get a range, say
     <code class="literal">20 &lt; i &lt;= 30</code>:
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
&lt;?php
$connection = new MongoClient();
$collection = $connection-&gt;database-&gt;collectionName;

$query = array( &#039;i&#039; =&gt; array( &#039;$gt&#039; =&gt; 20, &quot;\$lte&quot; =&gt; 30 ) );
$cursor = $collection-&gt;find( $query );

while ( $cursor-&gt;hasNext() )
{
    var_dump( $cursor-&gt;getNext() );
}
?&gt;
</pre></div>
    </div>
   </div>
   <p class="para">
    Remember to always escape the $-symbol or use single quotes.  Otherwise PHP
    will interpret it to be the variable <var class="varname">$gt</var>.
   </p>
  </div></div></div></body></html>